﻿using CodeGenerator.Model;
using RazorEngine;
using RazorEngine.Templating;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;


namespace CodeGenerator.Common
{
    public class EntityBuilder
    {
        private string _className;
        private List<ColumnInfo> _columns;
        private string _dbType;
        private bool _isSZMDX;
        private bool _isView;
        private string _nameSpace;
        private string _tableName;

        public EntityBuilder(string tableName, string nameSpace, string className, List<ColumnInfo> columns, bool isView) : this(tableName, nameSpace, className, columns, isView, false, null)
        {
        }

        public EntityBuilder(string tableName, string nameSpace, string className, List<ColumnInfo> columns, bool isView, bool isSZMDX, string dbType = null)
        {
            this._columns = new List<ColumnInfo>();
            this._nameSpace = "CodeGenerator.Model";
            this._isView = false;
            this._isSZMDX = isSZMDX;
            this._className = Utils.ReplaceSpace(className);
            this._nameSpace = Utils.ReplaceSpace(nameSpace);
            this._tableName = tableName;
            this._dbType = dbType;
            if (this._isSZMDX)
            {
                this._className = Utils.ToUpperFirstword(this._className);
            }
            this._isView = isView;
            foreach (ColumnInfo info in columns)
            {
                info.ColumnName = Utils.ReplaceSpace(info.ColumnName);
                if (this._isSZMDX)
                {
                    info.ColumnName = Utils.ToUpperFirstword(info.ColumnName);
                }
                info.Description = Utils.ReplaceSpace(info.Description);
                this._columns.Add(info);
            }
        }

        public string Builder(string tplContent)
        {
            this.Columns = DbToCS.DbtoCSColumns(this.Columns, this.DbType);
            if (!string.IsNullOrWhiteSpace(tplContent))
            {
                List<ColumnInfo> list = this.Columns.FindAll(col => col.IsPrimaryKey);
                ColumnInfo info = this.Columns.Find(col => col.IsIdentity);
                StringPlus plus = new StringPlus();
                plus.AppendLine("/********************************************************************************");
                plus.AppendLine("*     此代码由工具生成,重新生成代码会覆盖原生版本");
                plus.AppendLine("*     运行时版本:" + Environment.Version.ToString());
                plus.AppendLine("*     生成时间："+DateTime.Now .ToString("yyyy-MM-dd HH:mm:ss"));
                plus.AppendLine("********************************************************************************/");
                plus.AppendLine();
                string str = Engine.Razor.RunCompile(tplContent, "templateKey", null, new
                {
                    NameSpace = this.NameSpace,
                    ClassName = this.ClassName,
                    TableName = this.TableName,
                    Columns = this.Columns,
                    PrimaryKeyColumns = list,
                    IdentityColumn = info
                });
                
                return (plus.ToString() + str);
            }
            return "请使用模板生成方式";
        }

        public string ClassName
        {
            get =>
                this._className;
            set
            {
                this._className = value;
            }
        }

        public List<ColumnInfo> Columns
        {
            get =>
                this._columns;
            set
            {
                this._columns = value;
            }
        }

        public string DbType
        {
            get =>
                this._dbType;
            set
            {
                this._dbType = value;
            }
        }

        public bool IsView
        {
            get =>
                this._isView;
            set
            {
                this._isView = value;
            }
        }

        public string NameSpace
        {
            get =>
                this._nameSpace;
            set
            {
                this._nameSpace = value;
            }
        }

        public string TableName
        {
            get =>
                this._tableName;
            set
            {
                this._tableName = value;
            }
        }



        public abstract class MyCustomTemplateBase<T> : TemplateBase<T>
        {
            protected MyCustomTemplateBase()
            {
            }

            public bool IsLast(object item, List<object> list) =>
                (list.FindIndex(d => d == item) == (list.Count - 1));
        }
    }
}
